package com.jhclothing.web.controller.inbound;

import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.core.page.TableDataInfo;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.inbound.domain.FabricInbound;
import com.jhclothing.inbound.service.IFabricInboundService;
import com.jhclothing.product.domain.Fabric;
import com.jhclothing.product.service.IFabricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 布料入库Controller
 *
 * @author zyp
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/inbound/fabric")
public class FabricInboundController extends BaseController {
    @Autowired
    private IFabricInboundService fabricInboundService;
    @Autowired
    private IFabricService fabricService;

    /**
     * 查询布料入库列表
     */
    @PreAuthorize("@ss.hasPermi('inbound:fabric:list')")
    @GetMapping("/list")
    public TableDataInfo list(FabricInbound fabricInbound) {
        startPage();
        List<FabricInbound> list = fabricInboundService.selectFabricInboundList(fabricInbound);
        return getDataTable(list);
    }

    /**
     * 导出布料入库列表
     */
    @PreAuthorize("@ss.hasPermi('inbound:fabric:export')")
    @Log(title = "布料入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FabricInbound fabricInbound) {
        List<FabricInbound> list = fabricInboundService.selectFabricInboundList(fabricInbound);
        ExcelUtil<FabricInbound> util = new ExcelUtil<>(FabricInbound.class);
        util.exportExcel(response, list, "布料入库数据");
    }

    /**
     * 获取布料入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('inbound:fabric:query')")
    @GetMapping(value = {"/", "/{fabricInboundId}"})
    public AjaxResult getInfo(@PathVariable(value = "fabricInboundId", required = false) Long fabricInboundId) {
        AjaxResult ajax = AjaxResult.success();
        List<Fabric> fabrics = fabricService.selectFabricAll();
        ajax.put("fabrics", fabrics);
        if (StringUtils.isNotNull(fabricInboundId)) {
            FabricInbound fabricinbound = fabricInboundService.selectFabricInboundByInboundId(fabricInboundId);
            ajax.put(AjaxResult.DATA_TAG, fabricinbound);
            ajax.put("fabricId", fabricinbound.getFabricId());
        }
        return ajax;
    }

    /**
     * 新增布料入库
     */
    @PreAuthorize("@ss.hasPermi('inbound:fabric:add')")
    @Log(title = "布料入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FabricInbound fabricInbound) {
        fabricInbound.setCreateBy(getUsername());
        return toAjax(fabricInboundService.insertFabricInbound(fabricInbound));
    }

    /**
     * 修改布料入库
     */
    @PreAuthorize("@ss.hasPermi('inbound:fabric:edit')")
    @Log(title = "布料入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FabricInbound fabricInbound) {
        fabricInbound.setUpdateBy(getUsername());
        String result = fabricInboundService.updateFabricInbound(fabricInbound);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }

    /**
     * 删除布料入库
     */
    @PreAuthorize("@ss.hasPermi('inbound:fabric:remove')")
    @Log(title = "布料入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fabricInboundIds}")
    public AjaxResult remove(@PathVariable Long[] fabricInboundIds) {
        String result = fabricInboundService.deleteFabricInboundByInboundIds(fabricInboundIds);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }
}
